<template>
  <div class="login">
    <div class="loginBox">
      <h2 class="loginH2">
        <strong>知心购</strong> 后台管理系统
      </h2>
      <div class="loginCon">
        <div class="titleDiv">
          <h3>知行合壹</h3>
          <p>以简御繁 知行合壹</p>
          <i class="el-icon-collection-tag"></i>
        </div>
        <el-form ref="loginForm" :rules="rules" :model="ruleForm">
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              v-model="ruleForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-button type="primary" class="loginBtn" @click="loginHandler('loginForm')">登录</el-button>
        </el-form>
      </div>
    </div>

    <div class="slideShadow" v-show="showSlide">
      <transition>
        <div class="slideSty animated bounce">
          <slide-verify @success="onSuccess" @fail="onFail" :w="350" :h="175" ref="slideDiv"></slide-verify>
          <div class="iconBtn">
            <i class="el-icon-circle-close" @click="showSlide = false"></i>
            <i class="el-icon-refresh" @click="refresh"></i>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
// import $axios from "@/api";
import { login } from "@/api/login";
import { mapMutations } from "vuex";
import SlideVerify from "@/components/SlideVerify/SlideVerify.vue";

export default {
  data() {
    return {
      showSlide: false,
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18个字符", trigger: "blur" }
        ]
      }
    };
  },
  components: {
    SlideVerify
  },
  methods: {
    ...mapMutations(["setToken", "setUsername"]),
    async fetchLogin() {
      let res = await login(this.ruleForm);
      let { status } = res.meta;
      if (status === 200) {
        // this.$store.commit("setToken", res.data.token);
        // this.$store.commit("setUsername",res.data. username);
        const { token, username } = res.data;
        this.setToken(token);
        this.setUsername(username);
        this.$router.replace("/home");
      } else {
        this.$message({
          showClose: true,
          message: res.meta.msg,
          type: "error"
        });
      }
    },
    loginHandler(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // console.log("success");
          // this.fetchLogin();
          this.showSlide = true;
        } else {
          console.log("error!!");
          return false;
        }
      });
    },
    // 滑动验证成功
    onSuccess() {
      this.showSlide = false;
      this.fetchLogin();
    },
    // 滑动验证失败
    onFail() {
      this.$message({
        message: "验证失败",
        type: "error"
      });
    },
    // 刷新滑动验证组件
    refresh() {
      this.$refs.slideDiv.refresh();
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  width: 100%;
  background: url(../../assets/pageBg/loginBg.jpg) no-repeat center center;
  background-size: 100% 100%;
  overflow: hidden;

  .loginBox {
    height: 455px;
    width: 550px;
    margin: 0 auto;
    position: relative;
    top: 50%;
    margin-top: -287px;

    .loginH2 {
      font-size: 38px;
      color: #fff;
      text-align: center;
    }

    .loginCon {
      margin-top: 30px;
      background: #eee;
      border-radius: 4px;

      .titleDiv {
        padding: 0 28px;
        background: #fff;
        position: relative;
        height: 120px;
        border-radius: 4px 4px 0 0;

        h3 {
          font-size: 22px;
          color: #555;
          font-weight: initial;
          padding-top: 23px;
        }

        p {
          font-size: 16px;
          color: #888;
          padding-top: 12px;
        }

        i {
          font-size: 48px;
          color: #ddd;
          position: absolute;
          right: 27px;
          top: 29px;
        }
      }

      .el-form {
        padding: 25px 25px 30px 25px;
        background: #eee;
        border-radius: 0 0 4px 4px;

        .loginBtn {
          width: 100%;
          background: #19b9e7;
        }
      }
    }
  }

  .slideShadow {
    position: fixed;
    top: 0;
    z-index: 999;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);

    .slideSty {
      position: absolute;
      width: 380px;
      height: 311px;
      background: #e8e8e8;
      border: 1px solid #dcdcdc;
      left: 50%;
      top: 50%;
      margin-left: -188px;
      margin-top: -176px;
      z-index: 99;
      border-radius: 5px;

      .slide-verify {
        margin: 13px auto 0 auto;
        width: 350px !important;
      }

      .slide-verify-slider {
        width: 100% !important;
      }

      .slide-verify-refresh-icon {
        display: none;
      }

      .iconBtn {
        padding: 9px 0 0 19px;
        color: #5f5f5f;
        border-top: 1px solid #d8d8d8;
        margin-top: 17px;

        i {
          font-size: 22px;
          cursor: pointer;
        }

        i:last-child {
          margin-left: 7px;
        }
      }
    }
  }
}
</style>